package com.xauto.pioneer.appuser.activity.takeCar;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.adapter.ParkingSpaceAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.PageInfo;
import com.xauto.pioneer.appuser.model.ParkingSite;
import com.xauto.pioneer.appuser.util.PreferencesUtils;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/***
 * 附近的还车网点界面
 * @author Lemon Mon 27 Feb 16:23
 */
public class ParkingSpaceActivity extends BaseActivity {

    @Bind(R.id.topBar) TopBar topBar;
    @Bind(R.id.list)
    PullToRefreshListView pullToRefreshListView;
    private ListView list;
    /** 还车列表数据适配器*/
    private ParkingSpaceAdapter adapter;
    /** 还车网点数据集合*/
    private List<ParkingSite> parkingSites;
    /** 订单编号*/
    private String car_order_no;
    /** 网点编号*/
    private String parking_site_no;
    String longitude_num;
    String latitude_num;
    String distance_range;
    @Override
    protected int getContentView() {
        return R.layout.ac_list_no_refresh;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_parking_space));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        list = pullToRefreshListView.getRefreshableView();
        list.setVerticalScrollBarEnabled(false);
        list.setDividerHeight(3);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                //当前点击还车网点
                final ParkingSite currentParkingSite =  adapter.getItem(position-1);
                new LHttpLib().returnCar(mContext,car_order_no, memberId, currentParkingSite.parking_site_no,longitude_num,
                        latitude_num, new BaseLHttpHandler(mContext, true, false) {
                            @Override
                            public void onSuccess(JSONStatus jsonStatus) {
                                super.onSuccess(jsonStatus);
                                //当前点击还车网点
                                Intent intent = new Intent();
                                intent.putExtra("parkingSite",currentParkingSite);
                                setResult(RESULT_OK,intent);
                                FoundCarActivity.ParkingStat=1;
                                finish();
                            }
                            @Override
                            public void onFailure(Throwable t, int errorNo, String strMsg) {
                                super.onFailure(t, errorNo, strMsg);
                                Intent intent = new Intent();
                                intent.putExtra("parkingSite",currentParkingSite);
                                setResult(RESULT_CANCELED,intent);
                                PreferencesUtils.putString(mContext,"Park_NO",currentParkingSite.getParking_site_no());
                                FoundCarActivity.ParkingStat=1;
                                finish();
                            }
                        });
             }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //TODO 刷新
                page = 1;
                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    load();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                }
            }
        });
    }

    @Override
    protected void initData() {
        car_order_no = getIntent().getStringExtra("car_order_no");
        //parking_site_no = getIntent().getStringExtra("parking_site_no");
        longitude_num= getIntent().getStringExtra("longitude_num");
        latitude_num= getIntent().getStringExtra("latitude_num");
        distance_range= getIntent().getStringExtra("distance_range");

        lHandler = new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                pullToRefreshListView.onRefreshComplete();
                JSONArray jsonArray = jsonStatus.data.optJSONArray("parking_site_list");
                isMore = jsonStatus.pageInfo.more == 1 ? true : false;
                if(jsonArray != null && jsonArray.length() > 0) {
                    parkingSites = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        parkingSites.add(new ParkingSite().parse(jsonArray.optJSONObject(i)));
                    }
                    refreshList();
                }else {
                    showNoDataView();
                }
            }
        };

        adapter = new ParkingSpaceAdapter(this, R.layout.item_parking_site);
        list.setAdapter(adapter);
        page = 1;
        load();
    }

    void load() {
        new LHttpLib().getReturnParkingSiteList(mContext, memberId, car_order_no, longitude_num,latitude_num,distance_range, new PageInfo(page, Constants.COUNT), lHandler);
    }

    void refreshList() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (page == 1){adapter.getDataList().clear();}
                adapter.getDataList().addAll(parkingSites);
                adapter.notifyDataSetChanged();
            }
        });
    }
    private void showNoDataView(){
        adapter.getDataList().clear();
        adapter.notifyDataSetChanged();
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_listview_empty, null);
        ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("当前无还车网点信息");
        ((ViewGroup)list.getParent()).addView(emptyView);

        list.setEmptyView(emptyView);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
